package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Set;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Autowired
    RoleDao roleDao;

    @Autowired
    PermissionDao permissionDao;



    /**
     * 根据用户名查询用户信息（用户基本信息，角色，权限）
     *  1. 根据用户名查询用户信息
     *  2. 根据用户id查询对应的角色信息
     *  3. 根据角色id查询对应的权限信息
     * @param username
     * @return
     */
    @Override
    public SysUser findByUsername(String username) {
//        1. 根据用户名查询用户信息(不包含角色和权限)
        SysUser sysUser = userDao.findByUsername(username);
        if(sysUser != null){
//        2. 根据用户id查询对应的角色信息
            Set<Role> roleSet = roleDao.findListByUserId(sysUser.getId());
            if(roleSet != null &&roleSet.size() > 0){
//        3. 根据角色id查询对应的权限信息
                for (Role role : roleSet) {
                    Set<Permission> permissionSet = permissionDao.findListByRoleId(role.getId());
                    //把权限集合赋值给角色
                    role.setPermissions(permissionSet);
                }
                //把角色集合赋值给用户
                sysUser.setRoles(roleSet);
            }
            return sysUser;
        }
        return null;
    }

    @Override
    public void add(Integer[] roleIds,SysUser sysUser) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String password = sysUser.getPassword();
        String encode = bCryptPasswordEncoder.encode(password);
        sysUser.setPassword(encode);
        userDao.add(sysUser);
        //维护用户和角色的关系
        if(roleIds!=null&&roleIds.length>0&&sysUser.getId()!=null){
            for (Integer roleId : roleIds) {
                    userDao.set(sysUser.getId(),roleId);
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //根据条件查询
        Page<SysUser> page=userDao.findByCondition(queryPageBean.getQueryString());

        //创建返回值
        return new PageResult(page.getTotal(),page);
    }

    @Override
    public void edit(Integer[] roleIds,SysUser sysUser) {
        if(sysUser.getId()!=null){
            userDao.edit(sysUser);
            //先删除该用户与角色的关系
            userDao.delAssociation(sysUser.getId());
            //在维护用户和角色的关系
            if(roleIds!=null&&roleIds.length>0){
                for (Integer roleId : roleIds) {
                    userDao.set(sysUser.getId(),roleId);
                }
            }
        }
    }

    @Override
    public SysUser findById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public void delById(Integer id) {

        userDao.delAssociation(id);
        userDao.delById(id);
    }

    @Override
    public Integer[] findRoleIdsById(Integer id) {
        return userDao.findRoleIdsById(id);
    }
}
